/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server.teste;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import server.domain.*;
import server.util.DBUtil;

/**
 *
 * @author fillipe
 */
public class ServerTest {

    public static void main(String[] args) {
      
        
        long idClient = insertClientExemple();
        long idOrder = insertOrderExemple(idClient);
        insertOrderItemsExemple(idOrder);
    }

    public static long insertClientExemple() {
        Client client = new Client();
        client.setName("Vanderson Nunes");
        client.setCpf("123456789");

        long idClient = (Long) saveExemple(client);
        System.out.println(">> Client saved ");
        return idClient;
    }

    private static long insertOrderExemple(long idClient) {
        Order order = new Order();
        order.setIssueDate(new Date());
        order.setTotalOrder(new BigDecimal(350.26));

        Client client = getClientExemplo(idClient);
        order.setClient(client);

        long idOrder = (Long) saveExemple(order);
        System.out.println(">> Order saved ");
        return idOrder;
    }

    private static void insertOrderItemsExemple(long idOrder) {
        List<Long> listProducts = new ArrayList<Long>();
        listProducts.add((Long) saveExemple(new Product("coca-cola", new BigDecimal(3.0), 100)));
        listProducts.add((Long) saveExemple(new Product("doritos", new BigDecimal(2.0), 500)));
        listProducts.add((Long) saveExemple(new Product("sonho de valsa", new BigDecimal(1.0), 2000)));

        Order order = getOrderExemplo(idOrder);
        for (Long idProduct : listProducts) {
            Product product = getProductExemplo(idProduct);
            PKOrderItem pkOrderItem = new PKOrderItem(order, product);
            int qtd = pkOrderItem.getProduct().getId().intValue() * 2;
            saveExemple(new OrderItem(
                    pkOrderItem,
                    qtd,
                    product.getPrice(),
                    product.getPrice().multiply(new BigDecimal(qtd)),
                    BigDecimal.ZERO));
        }
    }

    private static Object saveExemple(Object obj) {
        Session session = DBUtil.getCurrentSession();
        Transaction t = session.beginTransaction();
        Object id = session.save(obj);
        t.commit();
        return id;
    }

    private static Client getClientExemplo(long id) {
        Session session = DBUtil.getCurrentSession();
        Transaction t = session.beginTransaction();
        Query query = session.createQuery("from Client c where c.id = :id");
        query.setParameter("id", id);
        Client client = (Client) query.uniqueResult();
        session.close();
        return client;
    }

    private static Order getOrderExemplo(long id) {
        Session session = DBUtil.getCurrentSession();
        Transaction t = session.beginTransaction();
        Query query = session.createQuery("from Order o where o.id = :id");
        query.setParameter("id", id);
        Order order = (Order) query.uniqueResult();
        session.close();
        return order;
    }

    private static Product getProductExemplo(long id) {
        Session session = DBUtil.getCurrentSession();
        Transaction t = session.beginTransaction();
        Query query = session.createQuery("from Product p where p.id = :id");
        query.setParameter("id", id);
        Product product = (Product)query.uniqueResult();
        session.close();
        return product;
    }
}
